﻿using Flow.Manage.Contracts;
using Framework.Models;
using Framework.Models.Entities;
using Framework.Models.Request;
using Framework.Models.Response;
using Framework.Repository;
using Framework.Utility.Extensions;
using Framework.Utility.Helper;
using Framework.Utility.ViewModels;
using Microsoft.EntityFrameworkCore;

namespace Flow.Manage.Service
{
    public class LeaveTimeService : ILeaveTimeContract
    {
        private readonly ILeaveTimeRepository _leaveTimeRepository;
        private readonly IFlowinstanceRepository _flowinstanceRepository;
        private readonly IWorkFlowContract _workFlowContract;

        public LeaveTimeService(ILeaveTimeRepository leaveTimeRepository,
            IFlowinstanceRepository flowinstanceRepository,
            IWorkFlowContract workFlowContract)
        {
            _leaveTimeRepository = leaveTimeRepository;
            _flowinstanceRepository = flowinstanceRepository;
            _workFlowContract = workFlowContract;
        }

        public async Task<TData<object>> GetLeaveTimeList()
        {
            var list = (from leave in _leaveTimeRepository.EntitiesAsNoTracking.Where(c => !c.IsDeleted)
                        join flow in _flowinstanceRepository.EntitiesAsNoTracking.Where(c => !c.IsDeleted)
                        on leave.FlowInstanceId equals flow.Id
                        select new Biz_LeaveTimeResponse
                        {
                            Id = leave.Id,
                            UserName = leave.UserName,
                            UserId = leave.UserId,
                            LeaveType = leave.LeaveType,
                            StartDate = leave.StartDate,
                            EndDate = leave.EndDate,
                            LeaveHour = leave.LeaveHour,
                            RequestComment = leave.RequestComment,
                            Attachment = leave.Attachment,
                            FlowInstanceId = leave.FlowInstanceId,
                            FlowNodeName = flow.ActivityName,
                            FlowStatus = flow.IsFinish,
                            CreateBy = leave.CreateBy,
                            CreateTime = leave.CreateTime,
                        });

            TData<object> tData = new TData<object>();
            tData.data = await list.ToListAsync();
            return tData;
        }

        public TData AddLeaveTime(Biz_LeaveTimeRequest param)
        {
            var leaveTime = param.MapTo<Biz_LeaveTime>();
            leaveTime.FlowInstanceId = SnowflakeId.Id;
            leaveTime.CreateTime = DateTime.Now;
            leaveTime.CreateBy = leaveTime.UserName;
            var count = _leaveTimeRepository.Insert(leaveTime);

            var reg = new RegisterFlow()
            {
                flowSchemeId = param.FlowSchemeId,
                flowInstanceId = leaveTime.FlowInstanceId,
                flowInstanceName = "请假申请-【" + leaveTime.UserName + "】" + DateTime.Now.Formatted("yyyy-MM-dd"),
                FlowLevel = (int)FlowLevel.Normal,
                applyUserId = leaveTime.UserId.ParseToString(),
                applyUserName = leaveTime.UserName,
            };

            if (count > 0 && !string.IsNullOrWhiteSpace(param.FlowSchemeId))
            {
                _workFlowContract.RegisterFlow(reg, leaveTime);
            }

            return count > 0 ? new TData(ResultTag.success) : new TData(ResultTag.fail);
        }

        public TData DeleteLeaveTime(int Id)
        {
            var count = _leaveTimeRepository.Delete(Id);
            return count > 0 ? new TData(ResultTag.success) : new TData(ResultTag.fail);
        }
    }
}